#ifndef OS_MASTER_FILE
#define OS_GLOBALS
#include "includes.h"
#endif

#if (TH)

#define _TEMP_TOTAL_NUM_ (161)
#define _ADC_REF_VOL_    (3.3)
#define _ADC_BIT_NUM_    (4096)  //2^12
#define _NTC_PULL_UP_    (10000) //resistor 10K
#define _TEMP_MIN_       (0000)
#define _TEMP_MAX_       (1600)

/********************************************************************/ /**
 * @brief:      Temprature table
 *
 * @param[in]:  NONE
 *
 * @return:     NONE
 *********************************************************************/
const uint16_t TempAD_Tab[_TEMP_TOTAL_NUM_] = {
    /*
  55240,//-10
  52348,//-9
  49626,//-8
  47065,//-7
  44653,//-6
  42379,//-5
  40236,//-4
  38214,//-3
  36306,//-2
  34504,//-1
  */
    32802, //0
    31194, //1
    29673, //2
    28234, //3
    26872, //4
    25583, //5
    24361, //6
    23204, //7
    22107, //8
    21067, //9
    20080, //10
    19143, //11
    18253, //12
    17408, //13
    16606, //14
    15843, //15
    15117, //16
    14427, //17
    13771, //18
    13146, //19
    12552, //20
    11986, //21
    11446, //22
    10933, //23
    10443, //24
    9976,  //25
    9538,  //26
    9122,  //27
    8727,  //28
    8351,  //29
    7993,  //30
    7653,  //31
    7329,  //32
    7021,  //33
    6728,  //34
    6448,  //35
    6181,  //36
    5927,  //37
    5685,  //38
    5454,  //39
    5234,  //40
    5023,  //41
    4823,  //42
    4631,  //43
    4448,  //44
    4273,  //45
    4106,  //46
    3946,  //47
    3793,  //48
    3647,  //49
    3508,  //50
    3374,  //51
    3246,  //52
    3124,  //53
    3006,  //54
    2894,  //55
    2787,  //56
    2684,  //57
    2585,  //58
    2490,  //59
    2400,  //60
    2313,  //61
    2229,  //62
    2150,  //63
    2073,  //64
    1999,  //65
    1929,  //66
    1861,  //67
    1796,  //68
    1733,  //69
    1673,  //70
    1615,  //71
    1560,  //72
    1507,  //73
    1455,  //74
    1406,  //75
    1359,  //76
    1313,  //77
    1269,  //78
    1227,  //79
    1187,  //80
    1148,  //81
    1110,  //82
    1071,  //83
    1039,  //84
    1006,  //85
    973,   //86
    942,   //87
    912,   //88
    883,   //89
    856,   //90
    829,   //91
    803,   //92
    778,   //93
    754,   //94
    731,   //95
    708,   //96
    687,   //97
    666,   //98
    646,   //99
    627,   //100
    608,   //101
    590,   //102
    572,   //103
    555,   //104
    539,   //105
    523,   //106
    508,   //107
    494,   //108
    479,   //109
    466,   //110
    452,   //111
    440,   //112
    427,   //113
    415,   //114
    404,   //115
    392,   //116
    381,   //117
    371,   //118
    361,   //119
    351,   //120
    341,   //121
    332,   //122
    323,   //123
    315,   //124
    306,   //125
    298,   //126
    290,   //127
    283,   //128
    275,   //129
    268,   //130
    261,   //131
    254,   //132
    248,   //133
    242,   //134
    236,   //135
    230,   //136
    224,   //137
    218,   //138
    213,   //139
    208,   //140
    202,   //141
    198,   //142
    193,   //143
    188,   //144
    184,   //145
    179,   //146
    175,   //147
    171,   //148
    167,   //149
    163,   //150
    159,   //151
    155,   //152
    152,   //153
    148,   //154
    145,   //155
    142,   //156
    138,   //157
    135,   //158
    132,   //159
    129,   //160
    /*
    126,//161
    123,//162
    121,//163
    118,//164
    115,//165
    113,//166
    110,//167
    108,//168
    106,//169
    103,//170
    */
};

/********************************************************************/ /**
 * @brief:      Get Temp Sensor Adc value
 *
 * @param[in]:  NONE
 *
 * @return:     NONE
 *********************************************************************/
uint16_t GetTemperatureAdcValue(void)
{
    uint8_t i;
    uint16_t adBuf[5];
    uint16_t adVal;

    for (i = 0; i < 5; i++) {
        adBuf[i] = ADConversion(ADC_Channel_17);
    }

    /*Min -> Max,taxis*/
    SortRoutine(adBuf, 5);

    /*delete Min and Max A/D value,calc average*/
    adVal = GetAverageValue(adBuf, 5);

    return adVal;
}

int16_t ReadTemperatureSensor(void)
{
    uint8_t i;
    int16_t temp = _TEMP_MIN_;
    uint16_t adVal;
    float Voltage;
    float NTC_Resistance;

    adVal = GetTemperatureAdcValue();

    Voltage        = adVal * TVGain * _ADC_REF_VOL_ / _ADC_BIT_NUM_;
    NTC_Resistance = Voltage * _NTC_PULL_UP_ / (_ADC_REF_VOL_ - Voltage);
    adVal          = (uint16_t)(NTC_Resistance / 10);

    //lock-up table
    for (i = 0; i < _TEMP_TOTAL_NUM_; i++) {
        if (adVal >= TempAD_Tab[i]) {
            break;
        }
        temp += 10;
    }

    if (i == 0) {
        temp = _TEMP_MIN_;
    } else if (i > _TEMP_TOTAL_NUM_) {
        temp = _TEMP_MAX_;
    } else {
        temp = temp - (adVal - TempAD_Tab[i]) / ((TempAD_Tab[i - 1] - TempAD_Tab[i]) / 10);
    }

    return temp;
}

#else

int16_t ReadTemperatureSensor(void);

#endif
